/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Staff;

/**
 *
 * @author Trang
 */
public class RoleDAO {
    private Connection con;

    public RoleDAO() {
        con = DBFactory.createConnection("onlineshopping");
    }
    
    //INSERT INTO `onlineshopping`.`staffrole` (`idStaff`, `idRole`) VALUES ('2', '1');
    public boolean InsertStaffRole(Integer idStaff, Integer idRole)
    {
        boolean result = false;
         try {
            //insert member account 1st
            
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date current = new Date();
            String sqlst1 = "INSERT INTO staffrole(idStaff, idRole) "
                    + "VALUE(?, ?)";

            PreparedStatement ps = con.prepareStatement(sqlst1);
            ps.setInt(1, idStaff);
            ps.setInt(2, idRole);    
            ps.executeUpdate();
            result = true;
        } catch (SQLException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }
    public Staff getDirect()
    {
        Staff result = new Staff();
        try
        {
            String sql = "SELECT staff.firstname,staff.idStaff, staff.lastname, staff.role " +
                          "FROM staffrole, staff "
                        +"Where staffrole.idRole= 11 and staffrole.idStaff = staff.idStaff LIMIT 1";
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
             while (rs.next())
            {
                Integer idStaff = rs.getInt("idStaff");
                
                String firstname = rs.getString("firstname");
                String lastname = rs.getString("lastname");
                String role = rs.getString("role");
                
                
                result = new Staff(idStaff, firstname, lastname, role);//new Staff();
                        //new Staff(idStaff, email, loginName, password, firstname, lastname);
            }
        }
                catch (SQLException ex) {
            Logger.getLogger(RoleDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
}
